//引入express模块
const express=require('express')
//引入连接池模块
const pool=require('../pool.js')
//console.log(pool)
//创建路由器对象
const r=express.Router()

// chneyang
// 加载bodyParser模块
const bodyParser = require("body-parser");

// 加载MD5模块
const md5 = require("md5");


//1.用户注册接口(post /reg)
//接口地址：http://127.0.0.1:3000/v1/users/reg
//请求方式：post

r.post('/reg',(req,res,next)=>{
	//获取post传递的参数
	var obj=req.body
	console.log(obj)
	//验证
	//手机号不能为空
	if(!obj.phone){//obj.uname===''
		//响应的同时，阻止往后执行
		return res.send({code:401,msg:'phone不能为空'})
	}
	//密码不能为空
	if(!obj.upwd){
		return res.send({code:402,msg:'upwd不能为空'})
	}
	
	
	//执行SQL命令，将数据插入到数据库
	pool.query('insert into xz_user set ?',[obj],(err,r)=>{
		if(err){
			//如果SQL中出现错误，交给下一个错误处理中间件，阻止往后执行
			return next(err)
		}
		console.log(r)
		if(r.length>0){
			res.send({code:200,msg:'注册成功'})
		}else{
			res.send({code:400,msg:'注册失败'})
		}
	})
})

//2.用户登录接口(get /login)
//接口地址：http://127.0.0.1:3000/v1/users/login
//请求方式：get
r.get('/login',(req,res,next)=>{
	var obj=req.query
	//手机号不能为空
	if(!obj.phone){//obj.uname===''
		//响应的同时，阻止往后执行
		return res.send({code:401,msg:'phone不能为空'})
	}
	//密码不能为空
	if(!obj.upwd){
		return res.send({code:402,msg:'upwd不能为空'})
	}
	//执行SQL命令,查询数据库
	pool.query('select * from xz_user where phone=? and upwd=?',[obj.phone,obj.upwd],(err,r)=>{
		if(err){
			return next(err)
		}
		console.log(r);
		if(r.length>0){
			res.send({code:200,msg:'登录成功'})
		}else{
			res.send({code:400,msg:'登录失败'})
		}
	})
})

// chenyang
// 获取所有用户的接口
r.get("/user", (req, res) => {
  // SQL语句以获取用户表的数据
  let sql = "SELECT * FROM xz_user limit ?,?";
  let pagenum = req.query.pagenum;
  let pagesize = req.query.pagesize;
  // 执行SQL语句
  pool.query(sql, [+pagenum - 1, +pagesize], (error, results) => {
    if (error) throw error;
    res.send({
      message: "ok",
      code: 200,
      results: results,
      total: results.length,
      pagesize: pagesize,
      pagenum: pagenum,
    });
  });
});
// 获取总条数接口
r.get("/count", (req, res) => {
  // SQL语句以获取用户表的数据
  let sql = "SELECT count(*) as total FROM xz_user";
  let count =
    // 执行SQL语句
    pool.query(sql, (error, results) => {
      if (error) throw error;
      res.send({
        message: "ok",
        code: 200,
        results,
      });
    });
});
// 获取指定用户的接口
r.get("/deuser", (req, res) => {
  // SQL语句以获取用户表的数据
  let sql = "SELECT * FROM xz_user where uname=?";
  // 执行SQL语句
  pool.query(sql, [req.query.uname], (error, results) => {
    if (error) throw error;
    console.log(results);
    res.send({
      message: "ok",
      code: 200,
      results: results,
      total: results.length,
    });
  });
});

// 增加用户的接口
r.get("/adduser", function (req, res) {
  const uname = req.query.uname;
  const upwd = req.query.upwd;
  const email = req.query.email;
  const phone = req.query.phone;
  // sql查询语句
  const sql = "insert into xz_user(uname,upwd,email,phone) values(?,?,?,?)";
  // 如果添加多个数据的话，用 [] 括起来
  pool.query(sql, [uname, upwd, email, phone], function (err, result) {
    console.log(sql);
    if (err) {
      throw err;
    }
    // res.send({message:'添加成功'})
    res.send({ message: "添加成功" });
  });
});
// 删除用户的接口
r.get("/del", (req, res) => {
  let uid = req.query.uid;
  let sql = "delete from xz_user where uid = ?";
  pool.query(sql, uid, (err, r) => {
    if (err) throw err;
    res.send({ code: 200, msg: "删除成功" });
  });
});

// 修改用户接口
r.get("/update", (req, res) => {
  let email = req.query.email;
  let uid = req.query.uid;
  let phone = req.query.phone;
  let user_name = req.query.user_name;
  let sql = "update xz_user set email=?,phone=?,user_name=? where uid = ?";
  pool.query(sql, [email, phone, user_name, uid], (err, r) => {
    if (err) throw err;
    res.send({ code: 200, msg: "修改成功" });
  });
});

//暴露路由器对象
module.exports=r